﻿namespace Neovolve.Toolkit.Workflow.Activities
{
    using System;
    using System.Activities;
    using System.Activities.Presentation;
    using System.Diagnostics;
    using System.Diagnostics.Contracts;
    using System.Drawing;
    using System.Windows;

    /// <summary>
    /// The <see cref="InstanceResolver"/>
    ///   class is used to provide a resolved instance for a child activity.
    /// </summary>
    [ToolboxBitmap(typeof(InstanceResolver), "brick.png")]
    public sealed class InstanceResolver : NativeActivity, IActivityTemplateFactory
    {
        /// <summary>
        /// Generates the display name.
        /// </summary>
        /// <param name="activityType">
        /// Type of the activity.
        /// </param>
        /// <param name="argumentCount">
        /// The argument count.
        /// </param>
        /// <returns>
        /// A <see cref="String"/> instance.
        /// </returns>
        public static String GenerateDisplayName(Type activityType, GenericArgumentCount argumentCount)
        {
            Contract.Requires<ArgumentNullException>(activityType != null);

            Type[] genericArguments = activityType.GetGenericArguments();
            String displayName = "InstanceResolver<" + genericArguments[0].Name;
            Int32 maxArguments = (Int32)argumentCount;

            if (maxArguments == 1)
            {
                return displayName + ">";
            }

            for (Int32 index = 1; index < maxArguments; index++)
            {
                displayName += ", " + genericArguments[index].Name;
            }

            return displayName + ">";
        }

        /// <summary>
        /// Creates the specified target.
        /// </summary>
        /// <param name="target">
        /// The target.
        /// </param>
        /// <returns>
        /// A <see cref="Activity"/> instance.
        /// </returns>
        [DebuggerNonUserCode]
        public Activity Create(DependencyObject target)
        {
            return
                new InstanceResolver
                    <Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object>();
        }

        /// <summary>
        /// Runs the activity’s execution logic.
        /// </summary>
        /// <param name="context">
        /// The execution context in which the activity executes.
        /// </param>
        protected override void Execute(NativeActivityContext context)
        {
            throw new NotSupportedException("This activity is intended to be used as a design time activity only. Use InstanceResolver instead.");
        }
    }
}